<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class AddBlackList
{
    protected $id;

    public function __construct($oid, $name, $is_black)
    {
        $this->id = $oid;
        $this->title = $name;
        $this->is_black = $is_black;
    }

    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        $('.AddBlackList').click(function () {
           var that = this;
           var id = $(that).attr('data-id');
           var title = $(that).attr('data-title');
           var index = layer.confirm('确定加入黑名单吗'+title+'?',{title:'提示'} ,function(index){
                $.ajax({
                    url: '/admin/common/black/add?id='+id,
                    type: 'get',
                    success: function(res){
                        layer.msg(res.text);
                        if(!res.code){
                         layer.close(index);
                         location.href = location.href;
                        }
                    }
                });
           });      
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        if($this->is_black == 1){
            return "&nbsp;<a href='javascript:void(0);'><button data-id='{$this->id}' data-title='{$this->title}' class='layui-btn layui-btn-normal layui-btn-xs'>已黑</button></a>";
        }else{
            return "&nbsp;<a href='javascript:void(0);'><button data-id='{$this->id}' data-title='{$this->title}' class='AddBlackList layui-btn layui-btn-danger layui-btn-xs'>加黑</button></a>";
        }

    }

    public function __toString()
    {
        return $this->render();
    }
}